package filescript;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycy
 * @CreateDate: 2018/11/20 15:56
 */
public class FileScriptWithText {

  public static void main(String[] args) {
    Map<Integer, List<String>> maphive = new FileScriptWithText().getMap("D:\\Users\\yincy\\Desktop\\hive_hotel");
    Map<Integer, List<String>> mapsql = new FileScriptWithText().getMap("D:\\Users\\yincy\\Desktop\\gl_hotel.txt");

    System.out.println("hive " + maphive.size());
    System.out.println("sql " + mapsql.size());

    int i = 0;
    int ii = 0;
    //查询hive表里有的,而sql中没有的标签
    for (Map.Entry<Integer, List<String>> integerListEntry : maphive.entrySet()) {
      Integer key = integerListEntry.getKey();
      List<String> hivetag = integerListEntry.getValue();
      List<String> sqltag = null;
      if ((sqltag = mapsql.get(key)) != null) {
        StringBuilder sb = new StringBuilder();
        boolean flag = false;
        //比较hive的标签和sql的标签
        for (String s : hivetag) {
          if (!sqltag.contains(s)) {
            i++;
            flag = true;
            sb.append(s).append(",");
          }
        }
        if (flag) {
          ii++;
          System.out.println("hive 里面新增的数据 " + key + ":" + sb);
        }

      }
    }

    System.out.println(i);
    System.out.println(ii);

  }


  public Map<Integer, List<String>> getMap(String filePath) {
    //hotelid,标签集合
    Map<Integer, List<String>> map = Maps.newHashMap();
    File file = new File(filePath);
    FileInputStream fis = null;
    BufferedReader br = null;
    int i = 0;
    try {
      Long start = System.currentTimeMillis();
      fis = new FileInputStream(file);
      br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
      while (br.ready()) {
        i++;
        String row = br.readLine();

        String[] rowArr = row.split("\t");
        if (rowArr.length == 2) {
          Integer hotelid = Integer.valueOf(rowArr[0]);
          String tags = rowArr[1];
          if (map.containsKey(hotelid)) {
            List<String> list = map.get(hotelid);
            for (String s : tags.split(",")) {
              list.add(s);
            }
          } else {
            List<String> list = Lists.newLinkedList();
            for (String s : tags.split(",")) {
              list.add(s);
            }
            map.put(hotelid, list);
          }
        } else {
          System.out.println("error" + rowArr[0]);
        }
      }
      System.out.println("cost " + (System.currentTimeMillis() - start));
      System.out.println("insert into row " + i + " 个");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {

    }

    return map;
  }

}
